package com.education.module.studentExamResult;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.education.domain.Certificate;
import com.education.domain.Student;
import com.education.domain.StudentExamResult;
import com.education.domain.User;
import com.education.framework.base.BaseController;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;
import com.education.framework.session.SessionHelper;
import com.education.module.studentExamResult.StudentExamResultServices;
import com.education.framework.util.calendar.CalendarUtil;

@Controller
@RequestMapping("studentExamResult")
public class StudentExamResultController extends BaseController{

	@Autowired
	private StudentExamResultServices services;
	private List<StudentExamResult> listResult;
	@RequestMapping(value = "")
	public String list(Model model, SearchParams searchParams,Page page,ServletRequest request){
		List<Certificate> certlist = services.findCert();
		List<Student> studentList = services.findStudentHasCert(searchParams,page);
		if(studentList.size()>0){
			for(Student student:studentList){
				 listResult = new ArrayList<StudentExamResult>();
				 for(Certificate certificate:certlist){
						 StudentExamResult studentExamResult= services.findStudentExamResult(student.getStudentId(),certificate.getId());
						 listResult.add(studentExamResult);
				 }
				 student.setListResult(listResult);
			}
		}
		
		model.addAttribute("certlist", certlist);
		model.addAttribute("studentList", studentList);
		model.addAttribute("page", page);
		model.addAttribute("searchParams", searchParams);
		return "/studentExamResult/studentExamResultList";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("action","create");
		return "/studentExamResult/studentExamResultEdit";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(StudentExamResult studentExamResult, Model model,RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		studentExamResult.setCreateUser(sessionUser);
		studentExamResult.setUpdateUser(sessionUser);
		studentExamResult.setCreateTime(CalendarUtil.getCurrentDate());
		services.save(studentExamResult);
		redirectAttributes.addFlashAttribute("studentExamResult", "SUCCESS");
		return "redirect:/studentExamResult";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Integer id, Model model) {
		StudentExamResult studentExamResult = services.findForObject(id);
		model.addAttribute("studentExamResult", studentExamResult);
		model.addAttribute("action", "update");
		return "/studentExamResult/studentExamResultEdit";
	}
	           
	@RequestMapping(value="checkPass",method=RequestMethod.GET)
	        public void ajax(@ModelAttribute StudentExamResult studentExamResult,PrintWriter printWriter) { 
			int res =services.updateResultByOne(studentExamResult.getResultId(),studentExamResult.getPass(),studentExamResult.getStudentId());
				if(res>0){
					  printWriter.write("0"); 
				}else{
					 printWriter.write("1"); 
				}
	        } 
	    
	  

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(StudentExamResult studentExamResult, RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		studentExamResult.setUpdateUser(sessionUser);
		services.update(studentExamResult);
		redirectAttributes.addFlashAttribute("studentExamResult", "SUCCESS");
		return "redirect:/studentExamResult";
	}
	
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Integer id,RedirectAttributes redirectAttributes) {
		services.deleteByStudentId(id);
		redirectAttributes.addFlashAttribute("studentExamResult", "SUCCESS");
		return "redirect:/studentExamResult";
	}
	
}
